﻿var bookmarks = function() {
    var removeBookmark = function(id, parent) {
        var url = rootUrl + "/DeleteBookmark/" + id;
        $.ajax({
            type: 'post',
            url: url,
            beforeSend: function() {
                parent.animate({
                    'font-weight': 'bold'
                },
                300);
            },
            success: function() {
                parent.slideUp(300, function() {
                    parent.remove();
                    bookmarks.flashMessage("Bookmark was removed.", "flashmessage");
                    loadlMenu();
                });
            }
        });
    };
    var removeLabel = function(id, parent) {
        var url = rootUrl + "/DeleteLabel/" + id;
        $.ajax({
            type: 'post',
            url: url,
            beforeSend: function() {
                parent.animate({
                    'font-weight': 'bold'
                },
                300);
            },
            success: function() {
                parent.slideUp(300, function() {
                    parent.remove();
                    bookmarks.flashMessage("Label was removed.", "flashmessage");
                    loadlMenu();
                });
            }
        });
    };
    var emailBookmark = function(id, dialog) {
        var emails = $("#email").val();
        var subject = $("#subject").val();
        var message = $("#message").val();
        var url = rootUrl + "/EmailBookmark/";
        $.ajax({
            type: 'POST',
            url: url,
            data: "id=" + id + "&emailto=" + emails + "&subject=" + subject + "&message=" + message,
            dataType: "json",
            success: function(msg) {
                if (msg.isSuccessfull) {
                    bookmarks.flashMessage(msg.message, 'flashmessage');
                    $.modal.close();
                }
                else { alert(msg.message) }

            }
        });

    };
    var loadlMenu = function() {
        var url = rootUrl + "/GetLabelMenus"
        $("#labelmenu-holder").load(url);
    }
    var confirm = function(message, callback) {
        $('#confirm').modal({
            close: false,
            position: ["20%", ],
            overlayId: 'confirmModalOverlay',
            containerId: 'confirmModalContainer',
            onShow: function(dialog) {
                dialog.data.find('.message').append(message);
                // if the user clicks "yes"
                dialog.data.find('.yes').click(function() {
                    // call the callback
                if ($.isFunction(callback)) {
                        callback.apply();
                    }
                    // close the dialog
                    $.modal.close();
                });
            }
        });
    };
    var emailModal = function(id, subject) {
        $('#sendemail').modal({
            close: false,
            position: ["20%", ],
            overlayId: 'confirmModalOverlay',
            containerId: 'emailModalContainer',
            onShow: function(dialog) {
                // if the user clicks "yes"
                bookmarks.contactsautocomplete();
                $("#subject").val(subject);
                dialog.data.find('.send').click(function() {
                    // call the callback
                    //validate
                    emailBookmark(id, dialog);
                    // close the dialog

                });
            }
        });
    };
    var buildmessage = function(message, id) {
        if ($("#" + id).length) {
            $("#" + id).text(message);
        }
        else {
            $("#header-top").append("<ul id=" + id + "><li>" + message + "</li></ul>");
        }
    };

    return {
        sendEmail: function() {
            $('a.email').click(function(e) {
                e.preventDefault();
                var id = $(this).attr("id").replace('e_', '');
                var subject = $("#sub_" + id).text();
                emailModal(id, subject);
            });
        },
        removeBookmarkConfirm: function() {

            $('a.confirm').click(function(e) {
                e.preventDefault();
                var id = $(this).attr("id").replace('d_', '');
                var parent = $("#b_" + id)
                confirm("Are you sure you want to delete the bookmark?", function() {
                    removeBookmark(id, parent);
                });
            });
        },
        removeLabelConfirm: function() {
            $(".edit").editable("ManageLabels", {
                tooltip: "Click to edit...",
                indicator: 'Saving...',
                style: "inherit",
                width: '50%'
            });
            $('a.delete').click(function(e) {
                e.preventDefault();
                var parent = $(this).parent();
                var id = $(this).attr("id").replace('label_', '');
                confirm("Are you sure you want to delete the label?", function() {
                    removeLabel(id, parent);
                });

            });
        },
        loadLabelMenu: function() {
            loadlMenu()
        },
        labelautocomplete: function() {
            $("#LabelNames").autocomplete(labellist, {
                width: 320,
                max: 4,
                highlight: false,
                multiple: true,
                multipleSeparator: ",",
                scroll: true,
                scrollHeight: 300
            });
        },
        contactsautocomplete: function() {
            $("#email").autocomplete(usercontacts, {
                width: 320,
                max: 4,
                highlight: false,
                multiple: true,
                multipleSeparator: ",",
                scroll: true,
                scrollHeight: 300
            });
        },
        displayMessage: function(message, id) {
            if (message.length > 0) {
                if ($("#" + id).length) {
                    $("#" + id).text(message);
                }
                else {
                    $("#message-container").append("<div id=" + id + ">" + message + "</div>");
                }
            }

        },

        flashMessage: function(message, id) {
            if (message.length > 0) {
                buildmessage(message, id)
                $("#" + id).fadeIn(function() {
                    setTimeout(function() {
                        $("#" + id).fadeOut(3600);
                    },
                2000);
                });
            }
        }
    }
} ();